package kino.command;

import kino.client.KinoClient;
import kino.common.KinoConstant;
import kino.database.KinoDB;
import kino.object.KinoObject;
import kino.object.KinoObjectTypeEnum;

import java.util.HashMap;
import java.util.Map;

@KinoCommandEnable
public class HlenCommand extends AbstractKinoCommand{
    @Override
    public String doExecute(KinoClient client, String... argv) {
        KinoDB db = client.getDb();
        Map<String, KinoObject> dict = db.getDict();
        String key = argv[0];
        if (dict.containsKey(key)) {
            KinoObject object = dict.get(key);
            if (object.getType() != KinoObjectTypeEnum.KINO_HASH) {
                return KinoConstant.wrongtypeerr;
            }
            return commandEncoder.encode(((Map)object.getObj()).size());
        }
        return commandEncoder.encode(0);
    }

    @Override
    public Integer getArgc() {
        return 1;
    }

    @Override
    public String getCommandName() {
        return "hlen";
    }

    @Override
    protected boolean expireCheck() {
        return true;
    }

    @Override
    public boolean aofFlag() {
        return false;
    }

    @Override
    public boolean hasExtParam() {
        return false;
    }
}
